package com.drools.demo.server;

import com.drools.demo.entity.Person;
import org.kie.api.runtime.KieContainer;
import org.kie.api.runtime.KieSession;
import org.kie.api.runtime.rule.QueryResults;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author czq
 * @Package com.drools.demo.server
 * @Date 2024/8/28 10:44
 * @description:
 */
@Service
public class QueryService {
    @Autowired
    private KieContainer kieContainer;

    public void executeRules() {
        Person person1 = new Person();
        person1.setAge(20);
        person1.setName("长");
        Person person2 = new Person();
        person2.setAge(10);
        person2.setName("登分数");

        KieSession kieSession = kieContainer.newKieSession();
        kieSession.getAgenda().getAgendaGroup("query_group1").setFocus();
        kieSession.insert(person1);
        kieSession.insert(person2);

        QueryResults result1 = kieSession.getQueryResults("query_rule1","长");
        result1.forEach(r -> System.out.println(r.get("$s")));

        kieSession.fireAllRules();
        kieSession.dispose();
    }
}
